Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Python -Dictionaries
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Python -Sets
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - set Operations

Sets in Python not only manage unique collections of items but also support various mathematical operations like union, intersection, and difference. These operations are useful for comparing sets and extracting relevant data based on these comparisons.

Union

The union of two sets is a set containing all elements from both sets, without duplicates. The union operation can be performed using the union() method or the | operator.

Example

Demonstrating the union of two sets.

Python3

. . . .

Explanation:

  • set1 and set2 contain overlapping and unique elements.
  • set1.union(set2) computes the union, resulting in {1, 2, 3, 4, 5}.
  • The | operator is an alternative that achieves the same result, showing the flexibility of set operations in Python.

Intersection

The intersection of two sets is a set containing only the elements that are common to both sets. The intersection operation can be performed using the intersection() method or the & operator.

Example

Demonstrating the intersection of two sets.

Python3

. . . .

Explanation:

  • set1.intersection(set2) computes the intersection, resulting in {2, 3}, which are the common elements in both sets.
  • The & operator provides a shorthand for the intersection method, yielding the same outcome.

Difference

The difference between two sets is a set containing elements that are in the first set but not in the second set. This operation helps in identifying unique elements in a set compared to another. The difference operation can be performed using the difference() method or the - operator.

Example

Demonstrating the difference between two sets.

Python3

. . . .

Explanation:

  • set1.difference(set2) calculates the difference, resulting in {1, 2}, which are the elements unique to set1 not found in set2.
  • The - operator is another way to express the difference, simplifying syntax while providing the same results.

These set operations are fundamental tools in Python for handling complex data relationships and analyses. By understanding how to perform unions, intersections, and differences, you can effectively manage and manipulate sets in your applications.

Introduction to set in Python
Python - Set Methods
Mark as Completed

On this page

Union

Example

Intersection

Example

Difference

Example